﻿using MaiPlanServer.IServices.Production;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.Production
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProcessDesignController : ControllerBase
    {
        public ProcessDesignController(IProcessDesignServices db)
        {
            Db = db;
        }

        public IProcessDesignServices Db { get; }
        /// <summary>
        /// 获取工序设计数据
        /// </summary>
        /// <param name="process_code"></param>
        /// <param name="process_name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProcess(int PageIndex,int PageSize, string process_code, string process_name, string status)
        {
            try
            {
                var list=await Db.GetPro_Processes(PageIndex,PageSize, process_code, process_name, status);

                var count=await Db.GetPro_ProcessesCount(process_code,process_name, status);

                return Ok(BaseState.CreateInstance(200, "请求成功", list,count));
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 新增工序
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        [HttpPost("AddProcess")]
        public async Task<IActionResult> AddProcess([FromForm]pro_process pro)
        {
            try
            {
                pro.process_id = Guid.NewGuid().ToString();
                bool bol=await Db.AddProcess(pro);

                if (bol)
                {
                    return Ok(BaseState.CreateInstance(200, "新增成功"));
                }
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 根据Id获取工序详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("GetProcess")]
        public async Task<IActionResult> GetProcess(string Id)
        {
            try
            {
                var data = await Db.GetPro_Processes(Id);
                var list = await Db.GetPro_ProcessContent(Id);

                return Ok(BaseState.CreateInstance(200, "请求成功",new {data,list }));
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 修改工序
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdateProcess([FromForm] pro_process pro)
        {
            try
            {
                bool bol = await Db.UpdatePro_Process(pro);

                if (bol)
                {
                    return Ok(BaseState.CreateInstance(200, "修改成功"));
                }
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }

        /// <summary>
        /// 新增操作步骤
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        [HttpPost("AddProcessContent")]
        public async Task<IActionResult> AddProcessContent([FromForm] pro_process_content pro)
        {
            try
            {
                pro.content_id = Guid.NewGuid().ToString();
                bool bol = await Db.AddProcessContent(pro);

                if (bol)
                {
                    return Ok(BaseState.CreateInstance(200, "新增成功"));
                }

            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 删除工序
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DelProcess(string Id)
        {
            try
            {
                bool bol = await Db.DelProcess(Id);

                if (bol)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("DelProcessContent")]
        public async Task<IActionResult> DelProcessContent(string Id)
        {
            try
            {
                bool bol = await Db.DelProcessContent(Id);

                if (bol)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
    }
}
